import { Component, OnDestroy, OnInit } from '@angular/core';
import { AutopartsService } from '@core/services/autoparts/autoparts.service';
import { ApiService } from '@core/services/http/api.service';
import { ApesMessageService } from '@shared/apes/message';
import { Subject, Subscription } from 'rxjs';
import { debounceTime } from 'rxjs/operators';
import { AutopartsViewService } from '@core/services/autoparts/autoparts.view.service';

@Component({
  selector: 'apes-exploded-views',
  templateUrl: './autoparts-dataCenter.component.html',
  styleUrls: ['./autoparts-dataCenter.component.scss'],
})

export class AutopartsDataCenterComponent implements OnInit, OnDestroy {

  isLoading: boolean = false;


  // select
  apesFilterOption = () => true;
  keyword: string = null;
  selectOption: Array<{ id: string; name: string }> = [];
  selectChange$ = new Subject(); //界面展示


  // parseVinSubscription: Subscription;
  changeShowTypeSubscription: Subscription;
  controlSearchSubscription: Subscription;
  controlShowSubscription: Subscription;
  selectSubscription: Subscription;

  showType: number = 1; // 主界面显示模式
  switchValue: boolean = false; // 查询界面显示模式
  carParam;

  // 车架号 配置车型 厂牌模式
  vin: string = '';
  vehicleConfig: string = '';
  brandName: string = '';

  // epc编码 epc版本模式
  epcId: string = '';
  epcVersion: string = '';


  // 关键词 目录名
  menuName: string = '';


  keywordAndMenuNameDisabled = true; //界面控制
  searchDisabled: boolean = false; // 查询控制
  showDisabled: boolean = true; // 界面切换控制

  constructor(
    private apiService: ApiService,
    private msg: ApesMessageService,
    private autopartsViewService: AutopartsViewService,
    private autopartsService: AutopartsService,
  ) {

  }

  ngOnInit(): void {

    this.changeShowTypeSubscription = this.autopartsViewService.showTypeChange$.subscribe((data: number) => this.showType = data);

    this.controlSearchSubscription = this.autopartsViewService.controlSearchBtnDisable$.subscribe((data: boolean) => this.searchDisabled = data);
    this.controlShowSubscription = this.autopartsViewService.controlShowBtn$.subscribe((data: boolean) => this.showDisabled = data);

    this.autopartsViewService.controlKeywordAndMenuNameDisable$.subscribe((data: boolean) => {
      this.keywordAndMenuNameDisabled = data;
    });

    this.autopartsViewService.clearKeywordAndMenuName$.subscribe( () => {
      this.keyword = null;
      this.menuName = '';
    });

    // this.parseVinSubscription = this.autopartsService.parseVinChanges$.subscribe(data => data);

    this.selectSubscription = this.selectChange$.pipe(debounceTime(500)).subscribe((data: string) => {
      this.apiService.post('autoparts.epcPartDescription.findAll', {
        'key': data,
        'page': 0,
        'size': 30,
        'group': {
          'filters': [
            {
              'field': 'id',
              'operator': 'EQ',
              'value': data,
            },
            {
              'field': 'name',
              'operator': 'LIKE',
              'value': data,
            },
          ],
          'groups': [],
          'operator': 'or',
        },
      }).subscribe(selectOption => {
        if (selectOption && selectOption.content && selectOption.content.length > 0) {
          this.selectOption = selectOption.content;
        } else {
          this.selectOption = [{ id: data, name: data }];
        }
      });
    });

    this.autopartsViewService.controlIsloading$.subscribe((data: boolean) => this.isLoading = data);
  }

  ngOnDestroy(): void {
    this.changeShowTypeSubscription.unsubscribe();
    this.controlSearchSubscription.unsubscribe();
    this.controlShowSubscription.unsubscribe();
    this.selectSubscription.unsubscribe();
  }

  toggle(type: number) {
    this.showType = type;
  }

  search() {
    this.autopartsViewService.clearKeywordAndMenuName$.next();
    this.switchValue ? this.storeEntry() : this.dataCenterEntry();
  }

  /***
   * 车架号 配置车型 厂牌入口
   */

  storeEntry() {
    if (!this.brandName.trim() || !this.vin.trim() || !this.vehicleConfig.trim()) {
      this.msg.error('车架号,配置车型编码,汽车厂牌名称都不允许为空');
      return false;
    }
    this.carParam = {
      vin: this.vin.trim(),
      vehicleConfig: this.vehicleConfig.trim(),
      brandName: this.brandName.trim(),
    };
    this.autopartsService.parseVin(this.carParam);
  }

  /***
   * epcId epcVersion 入口
   */
  dataCenterEntry() {
    if (!this.epcId.trim() || !this.epcVersion.trim()) {
      this.msg.error('EPC编码，EPC版本都不允许为空');
      return false;
    }
    this.carParam = { epcId: this.epcId.trim(), epcVersion: this.epcVersion.trim() };
    this.autopartsService.parseEpcIdAndEpcVersion(this.carParam);
  }

  /***
   * 查询配置车型
   */
  searchCarConfig() {

  }

  /***
   * 查询关键词
   */
  searchKeyword() {

  }

  /***
   * 查询末级目录
   */
  searchMenuName(value) {
    this.keyword = null;
    if (value.target.value) {
      this.autopartsService.searchMenuName(value.target.value);
    }
  }


  selectSearch(value: string) {
    if (value == null || value === '') {
      return;
    }
    this.selectChange$.next(value);
  }

  /***
   * 选中项目
   * @param value
   */
  selectChoose(value) {
    this.menuName = '';
    this.autopartsService.searchKeyword(value);
  }

  nextSubgroup() {
    this.autopartsService.nextSubgroup$.next({});
  }

  onSwitchSearch() {

  }

  onSwitchMainGroupMode() {

  }
}
